var RegisterHandler = function (){

}

RegisterHandler.prototype.listenSendCaptchaEvent = function (){
    var callback = function(event){
        var $this = $(this);
        //阻止默认事件
        event.preventDefault();
        console.log("发送验证码");
        var email = $("input[name='email']").val();
        var reg = /^\w+((.\w+)|(-\w+))@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+).[A-Za-z0-9]+$/;
        if(!email || !reg.test(email)) {
            alert("请输入正确的邮箱！");
            return;
        }
        forum.get({
            url : "/email/captcha?email="+email,
            success:function (result){
                if (result['code']===200) {
                    console.log("邮件发送成功！");
                    //取消按钮点击事件
                    $this.off('click');
                    //添加禁用
                    $this.attr('disabled','disabled');
                    // 倒计时
                    var countdown = 60;
                    let timer = setInterval(function (){
                        if(countdown>0){
                            $this.text(countdown);
                        }else{
                            $this.text("发送验证码");
                            $this.attr("disabled",false).on('click',callback);
                            clearInterval(timer);
                        }
                        countdown--;
                    },1000)

                }else{
                    var message = result['message'];
                    alert(message)
                }
            }
        })
    }
    $("#email-captcha-btn").on("click",callback)
}

RegisterHandler.prototype.listenGraphCaptchaEvent = function(){
    $("#captcha-img").on('click',function(){
        var $this = $(this);
        var src = $this.attr("src");
        let new_src = zlparam.setParam(src,"sign",Math.random());
        $this.attr("src",new_src);
    });
}

RegisterHandler.prototype.listenSubmitEvent = function(){
    $("#submit-btn").on('click',function (event){
        event.preventDefault();
        var email = $("input[name='email']").val();
        var email_captcha = $("input[name='email-captcha']").val();
        var username = $("input[name='username']").val();
        var password = $("input[name='password']").val();
        var repeat_password = $("input[name='repeat-password']").val();
        var graph_captcha = $("input[name='graph-captcha']").val();

        forum.post({
            url:"/register",
            data:{
                email,
                email_captcha,
                username,
                password,
                repeat_password,
                graph_captcha
            },
            success:function (result){
                if (result['code'] ===200){
                    window.location = '/login';
                }else{
                    alert(result['message']);
                }


            }
        })
    })
}

RegisterHandler.prototype.run = function (){
    this.listenSendCaptchaEvent();
    this.listenGraphCaptchaEvent();
    this.listenSubmitEvent();
}

$(function(){
    var handler = new RegisterHandler();
    handler.run();
})